package iut.acy.retard;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.EditText;

public class ConnectTask extends AsyncTask<Object, Void, String> {
	
	String response;
	EditText responseField;
	@Override
	protected String doInBackground(Object... params) {
		//responseField = (EditText) params[3];
		HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost((String) params[0]);
        
        ArrayList<NameValuePair> queryParams = new ArrayList<NameValuePair>();
        queryParams.add(new BasicNameValuePair("nom", ((EditText)params[1]).getText().toString()));
        queryParams.add(new BasicNameValuePair("prenom", ((EditText)params[2]).getText().toString()));
        queryParams.add(new BasicNameValuePair("heure", ((EditText)params[3]).getText().toString()));
        queryParams.add(new BasicNameValuePair("duree", ((EditText)params[4]).getText().toString()));
        queryParams.add(new BasicNameValuePair("taf", ((EditText)params[5]).getText().toString()));
        queryParams.add(new BasicNameValuePair("password", "gaillard666"));

        // create and launch the POST request
        try {
			httppost.setEntity(new UrlEncodedFormEntity(queryParams));
	        HttpResponse httpResponse;
			httpResponse = httpclient.execute(httppost);
			response = EntityUtils.toString(httpResponse.getEntity());
			/*JSONObject jsonResponse = new JSONObject(jsonString);
			response = jsonResponse.get("message").toString();*/
		} catch (UnsupportedEncodingException e) {
			//Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
		}  catch (ClientProtocolException e) {
			//Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
		} catch (IOException e) {
			//Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
		}  catch (ParseException e) {
			
			//Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
		} //catch (JSONException e) {
			//Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
		//}
        return null;
	}
	
	protected void onPostExecute(String result) {
        ConfirmDialog.verif(this.response);
    }

}
